//InnerAudioContext实例
//const app = getApp()
//InnerAudioContext实例
//var audioCxt;
//动画
//var audioAnimation;
//audioCxt = wx.createInnerAudioContext();
//audioCxt.src = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/kata.mp3';
var startPoint;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tips: {
      type: Array,
      value: [],
      observer: function (n) {
        if (n.length) {
          n.forEach(item => {
            if (item.type == 'warn') {
              item.w = item.tip_content.length * 40;
            } else {
              item.w = item.btn.length * 120;
            }

          })
          this.setData({
            tipsArr: n
          })
        }
      }
    },
    callbackFn: {
      type: Function,
      value: () => {}
    },
    tip0: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip0: n
          })
        }
      }
    },
    tip1: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip1: n
          })
        }
      }
    },
    tip2: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip2: n
          })
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    windowHeight: '',
    windowWidth: '',
    top1: 185,
    left1: 290,
    top2: 255,
    left2: 356,
    top3: 350,
    left3: 250,
    top4: 450,
    left4: 356,

    top4ok: 68,
    left4ok: 38,
    top3ok: 155,
    left3ok: 40,
    top2ok: 228,
    left2ok: 58,
    top1ok: 305,
    left1ok: 51,
    count:0,
  },
  /**
  * 页面加载完成执行
  */
 lifetimes:{
  ready(){
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          //buttonTop1:buttonTop1,//这里定义按钮的初始位置
          //buttonLeft:res.windowWidth*0.70,//这里定义按钮的初始位置
        })
      }
    })
    

  },
  attached: function() {
    // 在组件实例进入页面节点树时执行
  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
},
 //组件所在页面的生命周期
 pageLifetimes: {
  show: function() {
    // 页面被展示
  },
  hide: function() {
    // 页面被隐藏
  },
  resize: function(size) {
    // 页面尺寸变化
  }
},
   /**
   * 组件的方法列表
   */
  methods: {
  //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
    console.log('buttonStart======startPoint'+JSON.stringify(startPoint))
  },
  buttonMove: function (e) {
    let pen = e.currentTarget.dataset.pen;
    //console.log('move '+pen)
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonTop = this.data.top1 + translateY
    var buttonLeft = this.data.left1 + translateX
    if(pen==2){
      buttonTop = this.data.top2 + translateY
      buttonLeft = this.data.left2 + translateX
    }
    else if(pen==3){
      buttonTop = this.data.top3 + translateY
      buttonLeft = this.data.left3 + translateX
    }
    else if(pen==4){
      buttonTop = this.data.top4 + translateY
      buttonLeft = this.data.left4 + translateX
    }
    //判断是移动否超出屏幕
    if (buttonLeft+50 >= this.data.windowWidth){
      buttonLeft = this.data.windowWidth-50;
    }
    if (buttonLeft<=0){
      buttonLeft=0;
    }
    if (buttonTop<=0){
      buttonTop=0
    }
    if (buttonTop + 50 >= this.data.windowHeight){
      buttonTop = this.data.windowHeight-50;
    }
    let top = 'top'+pen;
    let left = 'left'+pen;
    console.log('move '+pen+' top='+buttonTop+' left='+buttonLeft)
    this.setData({
      [top]: buttonTop,
      [left]: buttonLeft
    })
  },
  buttonEnd: function (e) {
    let pen = e.currentTarget.dataset.pen;
    this.data.startPoint = e.changedTouches[0];
    let top = 'top'+pen;
    let left = 'left'+pen;
    let topok = 'top'+pen+'ok';
    let leftok = 'left'+pen+'ok';
    let buttonTop = 0;
    let buttonLeft = 0;
    let buttonTopOK = 0;
    let buttonLeftOK = 0;
    let offset = 30;
    this.data[top] = this.data.startPoint.clientY;
    this.data[left] = this.data.startPoint.clientX;
      buttonTop = this.data[top];
      buttonLeft = this.data[left];
      buttonTopOK = this.data[topok];
      buttonLeftOK = this.data[leftok];
    console.log('end '+pen+' top='+buttonTop+' left='+buttonLeft+' count='+this.data.count)
    
    if(buttonTop<=buttonTopOK+offset && buttonTop>=buttonTopOK-offset && buttonLeft<=buttonLeftOK+offset && buttonLeft>=buttonLeftOK-offset){
      
      this.data.count++;
      if(this.data.count==4){
        wx.showToast({
          title: '操作成功！',  // 标题
          icon: 'success',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
        })
      }
      console.log('end OK '+pen+' top='+buttonTop+' left='+buttonLeft+' count='+this.data.count)
    }
    else{
      if(this.data.count>4){
      this.data.count--;
      }
    }




  },
  /*
     * 内部私有方法建议以下划线开头
     * triggerEvent 用于触发事件
     */
    _okEvent(){
      //触发取消回调
      var that = this;
      console.log('触发取消回调')
      var isOk = 0;
      if(that.data.count>=4){
        isOk = 1;
      }
      return isOk;
      //this.triggerEvent("cancelEvent")
    },
    playReset:function(){
      this.setData({
        count:0,
        top1: 185,
        left1: 290,
        top2: 255,
        left2: 356,
        top3: 350,
        left3: 250,
        top4: 450,
        left4: 356,
      })
    },
  resetKey(){
    this.setData({
      isok:'',
      play1:0,
      play2:0,
      play3:0,
      play4:0,
    })
    wx.redirectTo({
      url: '../lock/test4',
    })
  },
  isOk(){
    let isok = this.data.isok;
    var words = isok.split(",");
    words = this.uniqueStr(words);
    isok = words.join(',');
    let tip = '再想想!';
    if(isok == '2,4,1,3,'){
      console.log('锁开了!')
      tip = '锁开了!';
    }
    else{
      console.log('2413再想想!'+ isok)
    }
    wx.showModal({
      title: '提示',
      content: tip,
      success: (res) => {
          // wx.switchTab({
          //   url: '/pages/strategyList/strategyList'
          // })                
      }
    })
  },
    //播放按钮事件
    playMusic : function(){
      audioCxt.play();
    },
  uniqueStr (array){
    return array.filter(function(el, index, arr) {
    return index == arr.indexOf(el);
    });
  },
  }
})